package face;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SpringLayout;

import model.Aula;
import model.Turma;
import repositorio.AulaRepositorio;
import repositorio.TurmaRepositorio;
public class GridDiarioClasse {

	JPanel panel;
	JComboBox cbTurmas;
	JButton	btBuscar;
	JButton	btNovaAula;
	JLabel label;
	JTable table;
	JScrollPane scroll;
	TurmaRepositorio turmaRepo;
	AulaRepositorio aulaRepo;
	Turma turmaSelec;
	 
	
	public GridDiarioClasse(){
		turmaRepo = new TurmaRepositorio();
		aulaRepo = new AulaRepositorio();
		
	}
	
	
	public JComponent card()
	{	
		JPanel panel = new JPanel(false);
		panel.setLayout(new GridLayout(1, 1));
		
		panel.add(criarInterface());
		
		return panel;
		
	}
	
	
	private JComponent criarInterface(){

		SpringLayout  layout = new  SpringLayout();
		
		panel = new JPanel(false);
		panel.setLayout(layout);
		
		table = new JTable();
		table.setShowGrid(true);
		scroll = new JScrollPane(table);
		
		label = new JLabel("Turma: ");
		cbTurmas = new JComboBox(turmaRepo.buscarTodos().toArray());
		btBuscar = new JButton("Buscar");
		btBuscar.addActionListener(
				new ActionListener() {
					public void actionPerformed(ActionEvent e) 
					{
						turmaSelec = (Turma) cbTurmas.getSelectedItem();
						table.setModel(new ChamadaDataModel(turmaSelec));
					}
				}
		);
		btNovaAula = new JButton("Nova Aula");
		btNovaAula.addActionListener(
				new ActionListener() {
					public void actionPerformed(ActionEvent e) 
					{
						
						criaNovaAula();
						
					}
				}
		);
		
		
        
		panel.add(label);
		panel.add(scroll);
		panel.add(cbTurmas);
		panel.add(btBuscar);
		panel.add(btNovaAula);
		
		
		configuraLayout(layout);
		
		
		return panel;
		
		
	}
	
	
	private void criaNovaAula() {
		
		
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		
		System.out.println(Calendar.DAY_OF_WEEK-1);
		System.out.println(turmaSelec.getDiaDaSemana().ordinal());
		
		if(Calendar.DAY_OF_WEEK-2 != turmaSelec.getDiaDaSemana().ordinal()){
			
			JOptionPane.showMessageDialog(null, 
					"Você só pode criar uma nova aula" +
					"na próximo(a) " + turmaSelec.getDiaDaSemana().getDescricao());

			return;
		}
		
		Aula novaAula = new Aula(turmaSelec);
		if(aulaRepo.incluir(novaAula)){
			
			List<Integer> matriculas = turmaRepo.buscarMatriculasDaTurma(turmaSelec);

			for (Integer matricula : matriculas) {
				
				if(aulaRepo.criarChamada(matricula, novaAula.getCdAula()))
					System.out.println("Aluno adicionado a chamada");
				
				else
					System.err.println("Aluno NAO adicionado a chamada");
				
				
			}
			
		}
		

	}
	
	
	
	
	private void configuraLayout(SpringLayout layout){
		
		 // Ajusta LABEL
        layout.putConstraint(
        		SpringLayout.WEST, label,
                5,
                SpringLayout.WEST, panel);
        layout.putConstraint(
        		SpringLayout.NORTH, label,
                5,
                SpringLayout.NORTH, panel);
        
        // Ajusta COMBO
        layout.putConstraint(
        		SpringLayout.WEST, cbTurmas,
                5,
                SpringLayout.EAST, label);
        layout.putConstraint(
        		SpringLayout.EAST, cbTurmas,
                -5,
                SpringLayout.WEST, btBuscar);
        layout.putConstraint(SpringLayout.NORTH, cbTurmas,
                5,
                SpringLayout.NORTH, panel);

        // Ajusta BUTTON BUSCAR
        layout.putConstraint(
        		SpringLayout.EAST, btBuscar,
                -5,
                SpringLayout.WEST, btNovaAula);
        layout.putConstraint(SpringLayout.NORTH, btBuscar,
                5,
                SpringLayout.NORTH, panel);
        
        // Ajusta BUTTON NOVA AULA
        layout.putConstraint(
        		SpringLayout.EAST, btNovaAula,
                -5,
                SpringLayout.EAST, panel);
        layout.putConstraint(SpringLayout.NORTH, btNovaAula,
                5,
                SpringLayout.NORTH, panel);

//         Ajusta TABLE
        layout.putConstraint(
        		SpringLayout.WEST, scroll,
                5,
                SpringLayout.WEST, panel);
        layout.putConstraint(SpringLayout.NORTH, scroll,
                2,
                SpringLayout.SOUTH, btNovaAula);
       
        // Ajusta TELA
        layout.putConstraint(
        		SpringLayout.EAST, panel,
                5,
                SpringLayout.EAST, scroll);
        layout.putConstraint(SpringLayout.SOUTH, panel,
                5,
                SpringLayout.SOUTH, scroll);
		
	}
	
	
	
	

	
	
	
	
}
